<?php
/**
 * 零起飞-(07FLY-CRM)
 * ==============================================
 * 版权所有 2015-2028   成都零起飞网络，并保留所有权利。
 * 网站地址: http://www.07fly.xyz
 * ----------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ==============================================
 * OaBusinessTripor: kfrs <goodkfrs@QQ.com> 574249366
 * Date: 2019-10-3
 */

namespace app\workflow\logic;

use think\Db;
use think\Request;

/**
 * 出差申请=》逻辑层
 */
class OaBusinessTrip extends WorkflowBase
{
    /**
     * 出差申请列表
     * @param array $where
     * @param bool $field
     * @param string $order
     * @param int|mixed $paginate
     * @return
     */
    public function getOaBusinessTripList($where = [], $field = true, $order = 'id desc', $paginate = DB_LIST_ROWS)
    {
        $list = $this->modelOaBusinessTrip->getList($where, $field, $order, $paginate)->toArray();
        if($paginate===false) $list['data']=$list;
        foreach ($list['data'] as &$row){
            $row['create_user_name']=$this->modelSysUser->getValue(['id'=>$row['create_user_id']],'realname');
            $row['status_arr']=$this->modelOaBusinessTrip->status($row['status']);
            //$row['status_arr']['action'] = $this->logicWorkflowBase->checkActionUrl($row['status_arr']['action']);
        }
        return $list;
    }

    /**
     * 出差申请添加
     * @param array $data
     * @return array
     */
    public function oaBusinessTripAdd($data = [])
    {
        $validate_result = $this->validateOaBusinessTrip->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateOaBusinessTrip->getError()];
        }
        $data['create_user_id']=SYS_USER_ID;
        $data['end_time']=date_calc($data['start_time'],"+".$data['days'],'day','Y-m-d H:i:s');
        $result = $this->modelOaBusinessTrip->setInfo($data);

        $result && action_log('新增', '新增出差信息');
        $url = url('show');
        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelOaBusinessTrip->getError()];
    }

    /**
     * 出差申请编辑
     * @param array $data
     * @return array
     */
    public function oaBusinessTripEdit($data = [])
    {

        $validate_result = $this->validateOaBusinessTrip->scene('edit')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateOaBusinessTrip->getError()];
        }
        $result = $this->modelOaBusinessTrip->setInfo($data);

        $url = url('show');
        $result && action_log('编辑', '编辑出差申请');
        return $result ? [RESULT_SUCCESS, '编辑成功', $url] : [RESULT_ERROR, $this->modelOaBusinessTrip->getError()];
    }

    /**
     * 出差申请删除
     * @param array $where
     * @return array
     */
    public function oaBusinessTripDel($data = [])
    {

        if (empty($data['id'])) {
            return [RESULT_ERROR, '选择删除的数据'];
            exit;
        }
        $ids_arr = str2arr($data['id']);
        foreach ($ids_arr as $id) {
            $condition['business_id'] = ['=', $id];
            $condition['business_type'] = ['=', 'oa_leave'];
            $this->modelWorkflowBusiness->deleteInfo($condition, true);
            $this->modelWorkflowBusinessHistory->deleteInfo($condition, true);
            $result = $this->modelOaBusinessTrip->deleteInfo(['id' => $id], true);
        }
        $result && action_log('删除', '删除出差申请' . http_build_query($data));

        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelOaBusinessTrip->getError()];
    }

    /**出差申请信息
     * @param array $where
     * @param bool $field
     * @return
     */
    public function getOaBusinessTripInfo($where = [], $field = true)
    {
        $info= $this->modelOaBusinessTrip->getInfo($where, $field);
        return $info;
    }


    /**
     * 请候状态
     * @return mixed
     * Author: kfrs <goodkfrs@QQ.com> created by at 2020/10/23 0023
     */
    public function  getStatus(){
        return $this->modelOaBusinessTrip->status();
    }

    /**
     * 出差类型
     * @param string $key
     * @return mixed
     * Author: kfrs <goodkfrs@QQ.com> created by at 2020/10/23 0023
     */
    public function  getType($key=''){
        return $this->modelOaBusinessTrip->type($key);
    }

    /**
     * 获取列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where = [];
        //关键字查
        !empty($data['keywords']) && $where['address|reason'] = ['like', '%' . $data['keywords'] . '%'];

        //状态
        if (!empty($data['status']) || is_numeric($data['status']) === true) {
            $where['status'] = ['=', $data['status']];
        }
        //状态
        if (!empty($data['type'])) {
            $where['type'] = ['=', $data['type']];
        }
        //创建时间
        if (!empty($data['create_rangedate'])) {
            $range_date = str2arr($data['create_rangedate'], "-");
            foreach ($range_date as &$date){
                $date=strtotime($date);
            }
            $where['create_time'] = ['between', $range_date];
        }
        //开始时间
        if (!empty($data['start_rangedate'])) {
            $range_date = str2arr($data['start_rangedate'], "-");
            $where['start_time'] = ['between', $range_date];
        }
        //结束时间
        if (!empty($data['end_rangedate'])) {
            $range_date = str2arr($data['end_rangedate'], "-");
            $where['end_time'] = ['between', $range_date];
        }
        return $where;
    }

    /**
     * 获取排序条件
     */
    public function getOrderBy($data = [])
    {
        $order_by = "";
        if (!empty($data['orderField'])) {
            $orderField = $data['orderField'];
            $orderDirection = $data['orderDirection'];
        } else {
            $orderField = "";
            $orderDirection = "";
        }
        if ($orderField == 'by_link') {
            $order_by = "link_time $orderDirection";
        } else if ($orderField == 'by_next') {
            $order_by = "next_time $orderDirection";
        } else if ($orderField == 'by_nodays') {
            $order_by = "nodays $orderDirection";
        } else {
            $order_by = "id desc";
        }
        return $order_by;
    }


    /**
     * 出差申请=》更新状态
     * @param array $data
     * @return array
     * Author: kfrs <goodkfrs@QQ.com> created by at 2020/9/18 0018
     */
    public function setOaBusinessTripStatus($data = [])
    {
        if (empty($data['status']) || empty($data['id'])) {
            return [RESULT_ERROR, '参数status不能为空'];
            exit;
        }
        $result = $this->modelOaBusinessTrip->setFieldValue(['id' => $data['id']], 'status', $data['status']);
        $result && action_log('编辑', '出差申请：' . $data['id']);
        return $result ? [RESULT_SUCCESS, '编辑成功', ''] : [RESULT_ERROR, $this->modelSalOrder->getError()];
    }

    /**
     * 出差申请=》审核提交
     * @param array $data
     * @return array
     * Author: kfrs <goodkfrs@QQ.com> created by at 2020/9/18 0018
     */
    public function setOaBusinessTripAuditSend($data = [])
    {
        if (empty($data['status']) || empty($data['id'])) {
            return [RESULT_ERROR, '参数status不能为空'];
            exit;
        }
        $intedata['business_type']='oa_business_trip';
        $intedata['business_id']=$data['id'];
        $res=$this->logicWorkflowBusiness->workflowAddInterface($intedata);
        if ($res[0] == RESULT_ERROR) return $res;
        $result = $this->modelOaBusinessTrip->setFieldValue(['id' => $data['id']], 'status', $data['status']);
        $result && action_log('提交', '提交出差审核：' . $data['id']);
        return $result ? [RESULT_SUCCESS, '提交出差审核', ''] : [RESULT_ERROR, $this->modelSalOrder->getError()];
    }


    /**
     * 请假申请=》审核提交
     * @param array $data
     * @return array
     * Author: kfrs <goodkfrs@QQ.com> created by at 2020/9/18 0018
     */
    public function setAuditSend($data = [])
    {
        if (empty($data['workflow_id']) || empty($data['id'])) {
            return [RESULT_ERROR, '选择申请流程不能为空'];
            exit;
        }
        $intedata['business_type']='oa_business_trip';
        $intedata['business_id']=$data['id'];
        $intedata['workflow_id']=$data['workflow_id'];
        $intedata['apply_user_id']=SYS_USER_ID;//业务申请人员
        $intedata['create_user_id']=SYS_USER_ID;//创建人员
        //$res=$this->logicWorkflowBusiness->workflowAddInterface($intedata);
        //内部接口调用=》提交看审批
        $rpcurl=url('workflow/rpc.RpcApi/workFlowBusinessAdd',$intedata);
        $res=httpRpc($rpcurl);
        if ($res['code'] == DATA_ERROR) return [RESULT_ERROR, $res['msg'], ''];
        $result = $this->modelOaBusinessTrip->setFieldValue(['id' => $data['id']], 'status', 1);
        $result && action_log('提交', '提交出差审核：' . $data['id']);
        return $result ? [RESULT_SUCCESS, '提交出差审核', ''] : [RESULT_ERROR, $this->modelSalOrder->getError()];
    }

    /**
     * 请假申请=》取消审批状态
     * @param array $data
     * @return array
     * Author: kfrs <goodkfrs@QQ.com> created by at 2020/9/18 0018
     */
    public function setAuditCancel($data = [])
    {
        if (empty($data['status']) || empty($data['id'])) {
            return [RESULT_ERROR, '参数status不能为空'];
            exit;
        }
        $result = $this->modelOaBusinessTrip->setFieldValue(['id' => $data['id']], 'status', $data['status']);
        $result && action_log('撤消', '取消审批：' . $data['id']);
        return $result ? [RESULT_SUCCESS, '取消成功', ''] : [RESULT_ERROR, $this->modelSalOrder->getError()];
    }

}
